<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String basePath = request.getScheme() + "://" + request.getServerName()
			+ ":" + request.getServerPort() + request.getContextPath();
%>
<jsp:include page="/WEB-INF/page/auth/_header.jsp"></jsp:include>
<body>
	<jsp:include page="/WEB-INF/page/auth/header.jsp"></jsp:include>
	<div id="middle">
		<jsp:include page="/WEB-INF/page/auth/menu.jsp"></jsp:include>
		<div id="right">
		    <div id="student_managetb">
		        <form id="student_manageForm" class="p10">
		            <span>${term.termName}</span>
		            <select class="form-control1" name="grade" id="grade" onchange="searchClazz('student_manage',this.value)"></select>
		            <select class="form-control1" name="clazz" id="clazz"></select>
		                             学生姓名：
		            <input class="form-control1" type="text" name="studentName" id="studentName">
					<input type="hidden" id="schoolId" name="schoolId" value=""/>
		            <a class="easyui-linkbutton l-btn l-btn-small" data-options="iconCls:'icon-search'" onclick="searchForm('student_manage')">查询</a>
		            <a class="easyui-linkbutton l-btn l-btn-small" data-options="iconCls:'icon-clear'" onclick="resetForm('student_manage')">重置</a>
		        </form>
		    </div>
		    <table id="student_managedg">
		    	<thead frozen="true">
		            <tr>
		                <th data-options="field:'ID',checkbox:true">ID</th>
		                <th data-options="field:'grade',title:'年级名称',formatter:function(value,row,index){
		                    return row.gradeName;
		                }"></th>
		                <th data-options="field:'clazz',title:'班号'"></th>
		                <th data-options="field:'studentNum',title:'学号'"></th>
		                <th data-options="field:'studyNo',title:'学籍号'"></th>
		                <th data-options="field:'name',title:'姓名'"></th>
		            </tr>
		        </thead>
		        <thead>
		            <tr>
		                <th data-options="field:'sex',title:'性别'"></th>
		                <th data-options="field:'idNoType',title:'身份证类型'"></th>
		                <th data-options="field:'idNo',title:'身份证号'"></th>
		                <th data-options="field:'birthday',title:'生日',
		                formatter:function(value,row,index) {
		                	return getTime(value,'yyyy/MM/dd');
		                }"></th>
		                <th data-options="field:'nation',title:'民族'"></th>
		                <th data-options="field:'nationality',title:'国籍'"></th>
		                <th data-options="field:'homeNature',title:'户口性质'"></th>
		                <th data-options="field:'placeType',title:'户籍类别'"></th>
		                <th data-options="field:'placeSSQ',title:'户籍省市区'"></th>
		                <th data-options="field:'placeDetail',title:'户籍所在详细地址'"></th>
		                <th data-options="field:'homeAddr',title:'现住地址'"></th>
		                <th data-options="field:'onlyChild',title:'是否独生子女'"></th>
		                <th data-options="field:'studentPlaceType',title:'户籍类别'"></th>
		                <th data-options="field:'nativePlace',title:'籍贯'"></th>
		                <th data-options="field:'birthAddr',title:'出生地'"></th>
		                <th data-options="field:'fatherName',title:'爸爸姓名'"></th>
		                <th data-options="field:'fatherMobile',title:'爸爸电话'"></th>
		                <th data-options="field:'fatherIsGuardian',title:'爸爸是否监护人'"></th>
		                <th data-options="field:'fatherPlace',title:'爸爸户口所在地'"></th>
		                <th data-options="field:'fatherHomeAddr',title:'妈妈现住址'"></th>
		                <th data-options="field:'motherName',title:'妈妈姓名'"></th>
		                <th data-options="field:'motherMobile',title:'妈妈电话'"></th>
		                <th data-options="field:'motherIsGuardian',title:'妈妈是否监护人'"></th>
		                <th data-options="field:'motherPlace',title:'妈妈户口所在地'"></th>
		                <th data-options="field:'motherHomeAddr',title:'妈妈现住址'"></th>
		            </tr>
		        </thead>
		    </table>
		</div>
	</div>
	<div id="confirmDialog" style="display: none;height: 'auto';">
		<form id="studentDialogForm" class="easyui-form p10" method="post" data-options="novalidate:true">
	    	<input id="id" name="id" type="hidden"/>
			<input type="hidden" id="schoolId" name="schoolId" value=""/>
	    	<table cellpadding="4">
	    		<tr>
	    			<td>姓名</td>
	    			<td><input class="easyui-textbox" type="text" name="name" id="name" data-options="required:true"></input></td>
	    			<td cellpadding="20">年级号</td>
	    			<td><input class="easyui-combobox" name="grade" id="grade" data-options="
					url:'<%=basePath %>/gradeCombo',
					method:'get',
					required:true,
					panelHeight:'auto'"></td>
	    		</tr>
	    		<tr>
	    			<td>班号</td>
	    			<td><input class="easyui-numberbox" name="clazz" id="clazz" required></td>
	    			<td>学号</td>
	    			<td><input class="easyui-numberbox" name="studentNum" id="studentNum" required></td>
	    		</tr>
	    		<tr>
	    			<td>性别</td>
	    			<td><select class="easyui-combobox w150" name="sex" id="sex" required>
	    				<option value="">请选择</option>
	    				<option value="男">男</option>
	    				<option value="女">女</option>
	    			</select></td>
	    			<td>登录密码</td>
	    			<td><input class="easyui-passwordbox" name="password" id="password"></td>
	    		</tr>
	    		<tr>
	    			<td>身份证类型</td>
	    			<td><select class="easyui-combobox w150" name="idNoType" id="idNoType" required>
	    				<option value="">请选择</option>
	    				<option value="居民身份证">居民身份证</option>
	    				<option value="香港特区护照/身份证明">香港特区护照/身份证明</option>
	    				<option value="澳门特区护照/身份证明">澳门特区护照/身份证明</option>
	    				<option value="台湾居民来往大陆通行证">台湾居民来往大陆通行证</option>
	    				<option value="境外永久居住证">境外永久居住证</option>
	    				<option value="护照">护照</option>
	    				<option value="港澳居民居住证">港澳居民居住证</option>
	    				<option value="台湾居民居住证">台湾居民居住证</option>
	    				<option value="其他">其他</option>
	    			</select></td>
	    			<td>身份证号</td>
	    			<td><input class="easyui-textbox" name="idNo" id="idNo" required></td>
	    		</tr>
	    		<tr>
	    			<td>出生日期</td>
	    			<td><input class="easyui-datebox" name="birthday" id="birthday"></td>
	    			<td>民族</td>
	    			<td><select class="easyui-combobox w150" name="nation" id="nation">
	    			</select></td>
	    		</tr>
	    		<tr>
					<td>国籍</td>
	    			<td><select class="easyui-combobox w150" name="nationality" id="nationality">
	    				<option value="">请选择</option>
	    				<option value="中国">中国</option>
	    				<option value="香港">香港</option>
	    				<option value="澳门">澳门</option>
	    				<option value="台湾">台湾</option>
	    				<option value="海外">海外</option>
	    			</select></td>
	    			<td>户口性质</td>
	    			<td><select class="easyui-combobox w150" name="homeNature" id="homeNature">
	    				<option value="">请选择</option>
	    				<option value="农业">农业</option>
	    				<option value="非农业">非农业</option>
	    			</select></td>
	    		</tr>
	    		<tr>
					<td>户籍类别</td>
	    			<td><select class="easyui-combobox w150" name="placeType" id="placeType">
	    				<option value="">请选择</option>
	    				<option value="广东省农村">广东省农村</option>
	    				<option value="广东省城镇">广东省城镇</option>
	    				<option value="非广东省农村">非广东省农村</option>
	    				<option value="非广东省城镇">非广东省城镇</option>
	    			</select></td>
	    			<td>户籍省市区</td>
	    			<td><input class="form-control" name="placeSSQ" id="placeSSQ"></td>
	    		</tr>
	    		<tr>
	    			<td>户籍所在详细地址</td>
	    			<td><input class="easyui-textbox" type="text" name="placeDetail" id="placeDetail"></input></td>
	    			<td>现住地址</td>
	    			<td><input class="easyui-textbox" type="text" name="homeAddr" id="homeAddr"></input></td>
	    		</tr>
	    		<tr>
					<td>是否独生子女</td>
	    			<td><select class="easyui-combobox w150" name="onlyChild" id="onlyChild">
	    				<option value="">请选择</option>
	    				<option value="是">是</option>
	    				<option value="否">否</option>
	    			</select></td>
	    			<td>籍贯</td>
	    			<td><input class="form-control" name="nativePlace" id="nativePlace"></td>
	    		</tr>
	    		<tr>
	    			<td>出生地</td>
	    			<td><input class="form-control" name="birthAddr" id="birthAddr"></td>
	    		</tr>
	    		<tr>
	    			<td>爸爸姓名</td>
	    			<td><input class="easyui-textbox" type="text" name="fatherName" id="fatherName"></input></td>
	    			<td>爸爸电话</td>
	    			<td><input class="easyui-numberbox" type="text" name="fatherMobile" id="fatherMobile"></input></td>
	    		</tr>
	    		<tr>
	    			<td>爸爸是否监护人</td>
	    			<td><select class="easyui-combobox w150" name="fatherIsGuardian" id="fatherIsGuardian">
	    				<option value="">请选择</option>
	    				<option value="是">是</option>
	    				<option value="否">否</option>
	    			</select></td>
	    			<td>爸爸户口所在地</td>
	    			<td><input class="form-control" name="fatherPlace" id="fatherPlace"></td>
	    		</tr>
	    		<tr>
	    			<td>爸爸现住址</td>
	    			<td><input class="form-control" name="fatherHomeAddr" id="fatherHomeAddr"></td>
	    		</tr>
	    		<tr>
	    			<td>妈妈姓名</td>
	    			<td><input class="easyui-textbox" type="text" name="motherName" id="motherName"></input></td>
	    			<td>妈妈电话</td>
	    			<td><input class="easyui-numberbox" type="text" name="motherMobile" id="motherMobile"></input></td>
	    		</tr>
	    		<tr>
	    			<td>妈妈是否监护人</td>
	    			<td><select class="easyui-combobox w150" name="motherIsGuardian" id="motherIsGuardian">
	    				<option value="">请选择</option>
	    				<option value="是">是</option>
	    				<option value="否">否</option>
	    			</select></td>
	    			<td>妈妈户口所在地</td>
	    			<td><input class="form-control" name="motherPlace" id="motherPlace"></td>
	    		</tr>
	    		<tr>
	    			<td>妈妈现住址</td>
	    			<td><input class="form-control" name="motherHomeAddr" id="motherHomeAddr"></td>
	    		</tr>
	    	</table>
	    </form>
	</div>
	
	<div id="uploadBox" style="display: none;height: 'auto';">
	    <div class="col-md-12 pd20">
   			<form id="uploadForm" novalidate="novalidate" enctype="multipart/form-data" method="post">
				<div class="form-group col-md-12" id="scoreTips">
					
				</div>
				<div class="form-group col-md-12 mt20">
					<input class="easyui-filebox form-control" id="files" name="files" style="width:250px;">
				</div>
			</form>
		</div>
	    <div class="text-center pd20">
	        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="uploadCommit()" style="width: 200px;">上传</a>
	    </div>
	</div>
	<jsp:include page="/WEB-INF/page/auth/_footer.jsp"/>
	<link rel="stylesheet" type="text/css" href="<%=basePath %>/css/weuix.min.css"/>
	<script src="<%=basePath %>/js/zepto.min.js" type="text/javascript"></script>
	<script src="<%=basePath %>/js/picker.js" type="text/javascript"></script>
    <script src="<%=basePath %>/js/picker-city.js" type="text/javascript"></script>
    <script src="<%=basePath %>/js/select.js" type="text/javascript"></script>
	<script type="text/javascript">
		var url = basePath + "/sys/student/manage";
        $(function(){
        	initSchoolIdInputValue();
        	loadDatagrid("student_manage",url);
            initGrade("student_manage");
            $('#files').filebox({
                buttonText: '选择文件',
                buttonAlign: 'left'
            });
            $("#nativePlace").cityPicker({
    	        title: "请选择省市区"
    	    });
    	    $("#placeSSQ").cityPicker({
    	        title: "请选择省市区"
    	    });
    	    $("#birthAddr").cityPicker({
    	        title: "请选择省市区"
    	    });
    	    $("#fatherPlace").cityPicker({
    	        title: "请选择省市区"
    	    });
    	    $("#fatherHomeAddr").cityPicker({
    	        title: "请选择省市区"
    	    });
    	    $("#motherPlace").cityPicker({
    	        title: "请选择省市区"
    	    });
    	    $("#motherHomeAddr").cityPicker({
    	        title: "请选择省市区"
    	    });
        });

		function initSchoolIdInputValue() {
			var v1 = $("#userSchoolId").val();
			$("#student_manageForm #schoolId").val(v1);
			$("#confirmDialog #schoolId").val(v1);
		}

		initNation("studentDialog");
        function loadDatagrid(cf, url) {
        	var btnArr=[];
			btnArr.push({text:'增加',iconCls:'icon-add',handler:function(){
				addStudent();	       
			}},'-');
			btnArr.push({text:'删除',iconCls:'icon-remove',handler:function(){
				del(cf, url);
			}},'-');
			btnArr.push({text:'修改',iconCls:'icon-edit',handler:function(){
				var dgObj=$('#student_managedg');
				var rows=dgObj.datagrid('getSelections');
				if(rows.length == 0) return;
				updateStudent(rows[0].id);
			}},'-');
			btnArr.push({text:'下载EXCEL模板',iconCls:'icon-save',handler:function(){downloadTemplate('学生信息采集模板.xls')}},"-");
			btnArr.push({text:'导入',iconCls:'icon-large-smartart',handler:function(){upload()}},"-");
			btnArr.push({text:'导出名单',iconCls:'icon-save',handler:function(){exportExcel(cf,url+'/export')}});
			$("#student_managedg").datagrid({
				url: url + "/pageQuery",
				rownumbers: true,
				striped: true,
				border: false,
				pagination: true,
				pageSize: 30,
				fitColumns: true,
				autoRowHeight: false,
				singleSelect: true,
				fit: true,
				toolbar: btnArr,
				queryParams : {
					schoolId: $("#userSchoolId").val()
				}
			});
			var tb = $("#"+cf+"tb").html();
			if(tb) {
				$(".datagrid-toolbar").prepend(tb);
				$("#"+cf+"tb").remove();
			}
		}
        function addStudent() {
        	loadForm();
        }
        function updateStudent(id) {
       		$.ajax({
       	 		url : url + "/queryById/" + id,
       	 		type : "GET",
       	 		dataType : "json",
       	 		async : false,
       	 		success : function(data) {
       	 			loadForm(1);
	       	 		$('#studentDialogForm #id').val(id);
	   	 			$('#studentDialogForm #clazz').textbox("setValue", data.clazz);
	   	 			$('#studentDialogForm #studentNum').textbox("setValue", data.studentNum);
	   	 			$('#studentDialogForm #name').textbox("setValue", data.name);
	       	 		$('#studentDialogForm #idNo').textbox("setValue", data.idNo);
	   	 			$('#studentDialogForm #birthday').textbox("setValue", data.birthday);
	   	 			$('#studentDialogForm #nation').textbox("setValue", data.nation);
	   	 			$('#studentDialogForm #nationality').textbox("setValue", data.nationality);
	   	 			$('#studentDialogForm #homeNature').textbox("setValue", data.homeNature);
		 			$('#studentDialogForm #placeType').textbox("setValue", data.placeType);
		 			$('#studentDialogForm #placeSSQ').val(data.placeSSQ);
	   	 			$('#studentDialogForm #placeDetail').textbox("setValue", data.placeDetail);
		 			$('#studentDialogForm #homeAddr').textbox("setValue", data.homeAddr);
		 			$('#studentDialogForm #birthAddr').val(data.birthAddr);
		 			$('#studentDialogForm #studentPlaceType').textbox("setValue", data.studentPlaceType);
		 			$('#studentDialogForm #nativePlace').val(data.nativePlace);
	   	 			$('#studentDialogForm #fatherName').textbox("setValue", data.fatherName);
		 			$('#studentDialogForm #fatherMobile').textbox("setValue", data.fatherMobile);
		 			$('#studentDialogForm #fatherPlace').val(data.fatherPlace);
		 			$('#studentDialogForm #fatherHomeAddr').val(data.fatherHomeAddr);
		 			$('#studentDialogForm #motherName').textbox("setValue", data.motherName);
		 			$('#studentDialogForm #motherMobile').textbox("setValue", data.motherMobile);
		 			$('#studentDialogForm #motherPlace').val(data.motherPlace);
		 			$('#studentDialogForm #motherHomeAddr').val(data.motherHomeAddr);
		 			$('#studentDialogForm #grade').combobox("setValue", data.grade);
		 			$('#studentDialogForm #sex').combobox("setValue", data.sex);
					$('#studentDialogForm #nation').combobox("setValue", data.nation);
					$('#studentDialogForm #idNoType').combobox("setValue", data.idNoType);
					$('#studentDialogForm #nationality').combobox("setValue", data.nationality);
					$('#studentDialogForm #homeNature').combobox("setValue", data.homeNature);
					$('#studentDialogForm #nationality').combobox("setValue", data.nationality);
					$('#studentDialogForm #onlyChild').combobox("setValue", data.onlyChild);
					$('#studentDialogForm #fatherIsGuardian').combobox("setValue", data.fatherIsGuardian);
					$('#studentDialogForm #motherIsGuardian').combobox("setValue", data.motherIsGuardian);
       	 		}
       	 	});
        }
        function resetForms(){
        	$('#studentDialogForm #id').val(null);
 			$('#studentDialogForm #grade').textbox("setValue", null);
 			$('#studentDialogForm #clazz').textbox("setValue", null);
 			$('#studentDialogForm #studentNum').textbox("setValue", null);
 			$('#studentDialogForm #name').textbox("setValue",null);
   	 		$('#studentDialogForm #idNo').textbox("setValue",null);
 			$('#studentDialogForm #birthday').textbox("setValue", null);
 			$('#studentDialogForm #nation').textbox("setValue", null);
 			$('#studentDialogForm #nationality').textbox("setValue", null);
 			$('#studentDialogForm #homeNature').textbox("setValue", null);
 			$('#studentDialogForm #placeType').textbox("setValue", null);
 			$('#studentDialogForm #placeSSQ').val(null);
 			$('#studentDialogForm #placeDetail').textbox("setValue", null);
 			$('#studentDialogForm #homeAddr').textbox("setValue", null);
 			$('#studentDialogForm #birthAddr').val(null);
 			$('#studentDialogForm #studentPlaceType').textbox("setValue", null);
 			$('#studentDialogForm #nativePlace').val(null);
 			$('#studentDialogForm #fatherName').textbox("setValue", null);
 			$('#studentDialogForm #fatherMobile').textbox("setValue", null);
 			$('#studentDialogForm #fatherPlace').val(null);
 			$('#studentDialogForm #fatherHomeAddr').val(null);
 			$('#studentDialogForm #motherName').textbox("setValue", null);
 			$('#studentDialogForm #motherMobile').textbox("setValue", null);
 			$('#studentDialogForm #motherPlace').val(null);
 			$('#studentDialogForm #motherHomeAddr').val(null);
 			$('#studentDialogForm #sex').combobox("setValue", null);
			$('#studentDialogForm #nation').combobox("setValue", null);
			$('#studentDialogForm #idNoType').combobox("setValue", null);
			$('#studentDialogForm #nationality').combobox("setValue", null);
			$('#studentDialogForm #homeNature').combobox("setValue", null);
			$('#studentDialogForm #nationality').combobox("setValue", null);
			$('#studentDialogForm #onlyChild').combobox("setValue", null);
			$('#studentDialogForm #fatherIsGuardian').combobox("setValue", null);
			$('#studentDialogForm #motherIsGuardian').combobox("setValue", null);
        }
        var loadForm = function(data){
        	confirmDialog = $('#confirmDialog').dialog({
        		title : data != null ? "修改学生信息" : "新增学生",
        		modal : true,
        		width : 800,
        		height: 600,
        		top : 60,
        		left: 220,
        		draggable : true,
        		resizable : true,
        		buttons : [{
					text:'确认',
					iconCls:'icon-ok',
					handler:function(){
						submitForm();
					}
				},{
					text:'取消',
					iconCls:'icon-cancel',
					handler:function(){
						$('#confirmDialog').dialog("close");
					}
				}],
        		onClose : function() {
        			resetForms();
        		}
        	}).show();
        };
        
        function submitForm() {
			var data = $.serializeObject($("#studentDialogForm"));
			if(!data.name) {
				$.messager.alert('提醒','姓名不能为空','info');
				return false;
			}
			else if(!data.idNoType) {
				$.messager.alert('提醒','证件类型不能为空','info');
				return false;
			}
			else if(!data.idNo) {
				$.messager.alert('提醒','身份证号不能为空','info');
				return false;
			}
			else if(!data.sex) {
				$.messager.alert('提醒','请选择性别','info');
				return false;
			}
			$.ajax({
				url: basePath + "/sys/student/manage/addUpdate",
				type: 'POST',
				data: data
			}).done(function(data) {
				if(data.code=='000'){
					$.messager.show({title:'操作提示', msg:"提交成功"});
					$('#confirmDialog').dialog("close");
					$('#student_managedg').datagrid('reload');
				}
			}).fail(function() {
				$('#msg').html('error,发生错误请及时联系管理员!');
			});
		}
        
        var uploadBox;
        function upload() {
        	$("#scoreTips").html("<span class='calendar-sunday'>确认信息无误？以免上传出错</span>");
        	uploadBox = $('#uploadBox').dialog({
        		title : '上传学生信息',
        		modal : true,
        		width : 400,
        		top : 200,
        		draggable : true,
        		resizable : true,
        		buttons : '#btns',
        		onClose : function() {
        			$('#files').filebox('setValue','');
        		}
        	}).show();
        }

        function uploadCommit(){
        	var fileObj = document.getElementById('filebox_file_id_2').files[0];
        	if(!fileObj) {
        		$.messager.alert('提醒','请选择文件');
        		return;
        	}
        	$.messager.progress({
        		title : '提示',
        		msg : '上传中，请稍候...'
        	});
        	var xhr = new XMLHttpRequest();
        	xhr.open('POST', basePath + "/sys/report/student/import");
        	xhr.onload = function () {
               if (xhr.status === 200) {
            	    $.messager.progress('close');
        	       	var resp = JSON.parse(xhr.response);
        	       	if(resp.code == '000') {
        	       		$.messager.alert("成功","上传成功！","info");
        	       		$('#student_managedg').datagrid('reload');
        	       		$('#files').filebox('setValue','');
        	       		uploadBox.dialog('close');
        	       	}else {
        	       		$.messager.alert('错误提醒', resp.msg);
        	       	}
               }
           };
           xhr.onerror = function () {
        	   $.messager.progress('close');
        	   $.messager.alert('错误', "上传异常");
           };
           var fd = new FormData();
           fd.append('files', fileObj);
           fd.append("termId", $("#termId").val());
           fd.append("termType", $("#termType").val());
           fd.append("grade", $("#grade").val());
           fd.append("clazz", $("#clazz").val());
           // 触发上传
        	setTimeout(function() {
        		xhr.send(fd);
        	}, 1000)
        }
    </script>
</body>
</html>